﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
using EntidadesCompartidas;
using System.Web.Services;

namespace Smart_House_System.Cliente
{
    public partial class tablerocliente : System.Web.UI.Page
    {

        private static ServiceReference2.Service1Client myService = null;
        private static Casa laCasa;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    laCasa = (Casa)Session["Casa"];
                    myService = new ServiceReference2.Service1Client();
                    myService.Endpoint.Address = new EndpointAddress(new Uri(laCasa.Servicio), myService.Endpoint.Address.Identity, myService.Endpoint.Address.Headers);
                    myService.Open();
                    myService.GetData(2);
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("~/Cliente/ErrorCliente.aspx?Mensaje=" + ex.Message);
            }
        }

        [WebMethod] // Atributo necesario para trabajar con servicios Webs 
        public static string sendData(string message)
        {
            try
            {
                System.Media.SoundPlayer player = new System.Media.SoundPlayer();
                switch (message)
                {
                    case "btn1":
                        myService.SetRequestAlarm("30 37 30 31 43 38 0D 0A");
                        myService.SetRequestAlarm("30 37 30 5E 46 35 0D 0A");
                        player.SoundLocation = @"C:\Smart_House_System\Smart_House_System\Sonidos\beep-6.wav";
                        player.Play();
                        break;
                    case "btn2":
                        myService.SetRequestAlarm("30 37 30 32 43 39 0D 0A");
                        myService.SetRequestAlarm("30 37 30 5E 46 35 0D 0A");
                        player.SoundLocation = @"C:\Smart_House_System\Smart_House_System\Sonidos\beep-6.wav";
                        player.Play();
                        break;
                    case "btn3":
                        myService.SetRequestAlarm("30 37 30 33 43 41 0D 0A");
                        myService.SetRequestAlarm("30 37 30 5E 46 35 0D 0A");
                        player.SoundLocation = @"C:\Smart_House_System\Smart_House_System\Sonidos\beep-6.wav";
                        player.Play();
                        break;
                    case "btn4":
                        myService.SetRequestAlarm("30 37 30 34 43 42 0D 0A");
                        myService.SetRequestAlarm("30 37 30 5E 46 35 0D 0A");
                        player.SoundLocation = @"C:\Smart_House_System\Smart_House_System\Sonidos\beep-6.wav";
                        player.Play();
                        break;
                    case "btn5":
                        myService.SetRequestAlarm("30 37 30 35 43 43 0D 0A");
                        myService.SetRequestAlarm("30 37 30 5E 46 35 0D 0A");
                        player.SoundLocation = @"C:\Smart_House_System\Smart_House_System\Sonidos\beep-6.wav";
                        player.Play();
                        break;
                    case "btn6":
                        myService.SetRequestAlarm("30 37 30 36 43 44 0D 0A");
                        myService.SetRequestAlarm("30 37 30 5E 46 35 0D 0A");
                        player.SoundLocation = @"C:\Smart_House_System\Smart_House_System\Sonidos\beep-6.wav";
                        player.Play();
                        break;
                    case "btn7":
                        myService.SetRequestAlarm("30 37 30 37 43 45 0D 0A");
                        myService.SetRequestAlarm("30 37 30 5E 46 35 0D 0A");
                        player.SoundLocation = @"C:\Smart_House_System\Smart_House_System\Sonidos\beep-6.wav";
                        player.Play();
                        break;
                    case "btn8":
                        myService.SetRequestAlarm("30 37 30 38 43 46 0D 0A");
                        myService.SetRequestAlarm("30 37 30 5E 46 35 0D 0A");
                        player.SoundLocation = @"C:\Smart_House_System\Smart_House_System\Sonidos\beep-6.wav";
                        player.Play();
                        break;
                    case "btn9":
                        myService.SetRequestAlarm("30 37 30 39 44 30 0D 0A");
                        myService.SetRequestAlarm("30 37 30 5E 46 35 0D 0A");
                        player.SoundLocation = @"C:\Smart_House_System\Smart_House_System\Sonidos\beep-6.wav";
                        player.Play();
                        break;
                    case "btnFleIz":
                        myService.SetRequestAlarm("30 37 30 3C 44 33 0D 0A");
                        myService.SetRequestAlarm("30 37 30 5E 46 35 0D 0A");
                        player.SoundLocation = @"C:\Smart_House_System\Smart_House_System\Sonidos\beep-6.wav";
                        player.Play();
                        break;
                    case "btnFleDe":
                        myService.SetRequestAlarm("30 37 30 3E 44 35 0D 0A");
                        myService.SetRequestAlarm("30 37 30 5E 46 35 0D 0A");
                        player.SoundLocation = @"C:\Smart_House_System\Smart_House_System\Sonidos\beep-6.wav";
                        player.Play();
                        break;
                    case "btnfuego":
                        myService.SetRequestAlarm("30 37 30 46 44 44 0D 0A");
                        myService.SetRequestAlarm("30 37 30 5E 46 35 0D 0A");
                        player.SoundLocation = @"C:\Smart_House_System\Smart_House_System\Sonidos\beep-6.wav";
                        player.Play();
                        break;
                    case "btnAlerta":
                        myService.SetRequestAlarm("30 37 30 41 44 38 0D 0A");
                        myService.SetRequestAlarm("30 37 30 5E 46 35 0D 0A");
                        player.SoundLocation = @"C:\Smart_House_System\Smart_House_System\Sonidos\beep-6.wav";
                        player.Play();
                        break;
                    case "btn0":
                        myService.SetRequestAlarm("30 37 30 30 43 37 0D 0A");
                        myService.SetRequestAlarm("30 37 30 5E 46 35 0D 0A");
                        player.SoundLocation = @"C:\Smart_House_System\Smart_House_System\Sonidos\beep-6.wav";
                        player.Play();
                        break;
                    case "btnAsterisco":
                        myService.SetRequestAlarm("30 37 30 2A 43 31 0D 0A");
                        myService.SetRequestAlarm("30 37 30 5E 46 35 0D 0A");
                        player.SoundLocation = @"C:\Smart_House_System\Smart_House_System\Sonidos\beep-6.wav";
                        player.Play();
                        break;
                    case "btnNumeral":
                        myService.SetRequestAlarm("30 37 30 23 42 41 0D 0A");
                        myService.SetRequestAlarm("30 37 30 5E 46 35 0D 0A");
                        player.SoundLocation = @"C:\Smart_House_System\Smart_House_System\Sonidos\beep-6.wav";
                        player.Play();
                        break;
                    case "btnPerHome":
                         break;
                    case "btnCampana":
                         break;
                    case "btnCasaVacia":
                         break;
                    case "btnCirculoFlechas":
                         break;
                }
                return "";
            }
            catch (Exception ex)
            {
                myService.Close();
                return "";
            }
        }

        protected void UpdateTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                string ret = myService.GetResponseAlarm();
                if (ret != pantallaTab.Text && ret != "")
                {
                    if (ret.Substring(0, 3) != "500" && ret.Substring(0, 3) != "905" &&
                        ret.Substring(0, 3) != "904" && ret.Substring(0, 3) != "903" &&
                        ret.Substring(0, 3) != "652" && ret.Substring(0, 3) != "655" &&
                        ret.Substring(0, 3) != "700" && ret.Substring(0, 3) != "652" &&
                        ret.Substring(0, 3) != "650" && ret.Substring(0, 3) != "651" &&
                        ret.Substring(0, 3) != "602" && ret.Substring(0, 3) != "673" &&
                        ret.Substring(0, 3) != "840")
                    {
                        pantallaTab.Text = ret;
                        if (ret.Contains("System is       Ready to Arm") || (ret.Contains("System Disarmed")))
                        {
                            imgCandado.ImageUrl = "..\\Imagenes\\candado_verde.jpg";
                            imgTick.ImageUrl = "..\\Imagenes\\tick_verde.jpg";
                            imgTriangulo.ImageUrl = "..\\Imagenes\\triangulo_negro.jpg";
                        }
                        if (ret.Contains("System Armed") || ret.Contains("Exit Delay"))
                        {
                            imgCandado.ImageUrl = "..\\Imagenes\\candado.jpg";
                            imgTick.ImageUrl = "..\\Imagenes\\tick_rojo.jpg";
                            imgTriangulo.ImageUrl = "..\\Imagenes\\triangulo_negro.jpg";
                        }
                        if (ret.Contains("System Armed"))
                        {
                            imgCandado.ImageUrl = "..\\Imagenes\\candado.jpg";
                            imgTick.ImageUrl = "..\\Imagenes\\tick_rojo.jpg";
                            imgTriangulo.ImageUrl = "..\\Imagenes\\triangulo_negro.jpg";
                        }
                        if (ret.Contains("in Alarm"))
                        {
                            imgCandado.ImageUrl = "..\\Imagenes\\candado.jpg";
                            imgTick.ImageUrl = "..\\Imagenes\\tick_rojo.jpg";
                            imgTriangulo.ImageUrl = "..\\Imagenes\\triangulo_amarillo.jpg";
                        }
                        if (ret.Contains("Secure System"))
                        {
                            imgCandado.ImageUrl = "..\\Imagenes\\candado_verde.jpg";
                            imgTick.ImageUrl = "..\\Imagenes\\tick_rojo.jpg";
                            imgTriangulo.ImageUrl = "..\\Imagenes\\triangulo_amarillo.jpg";
                        }
                        //if (ret.Substring(0, 3) == "609" || ret.Substring(0, 3) == "610")
                        //{
                        //    int i = 0;
                        //}
                    }
                }
            }
            catch (EndpointNotFoundException ex2)
            {
                Response.Redirect("~/Cliente/ErrorCliente.aspx?Mensaje=" + ex2.Message);
            }
        }

    }
}